package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * 查找今日佳人实现类
 */
@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryUserIdMaxScore(Long toUserId) {

        //1.构造查询条件,查询与toUserId这个操作用户缘分最高的用户
        Criteria criteria =Criteria.where("toUserId").is(toUserId);
        //2.创建query对象
        Query query = Query.query(criteria);

        query.with(Sort.by(Sort.Order.desc("score")))
              .limit(1);

        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }




    /**推荐好友的分页查询
     *
     * @param page
     * @param pagesize
     * @param toUserId
     * @return
     */
    public List<RecommendUser> queryRecommendFriends(Integer page, Integer pagesize, Long toUserId) {

        //1.构造Criter对象，设置查询条件
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2.根据Criteria创建Query对象
        Query query = Query.query(criteria);
        //3.查询总记录数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //4.设置分页查询条件
        query.with(Sort.by(Sort.Order.desc("score"))).skip((page - 1) * pagesize).limit(pagesize);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        //5.构造返回数据


        return recommendUsers;
    }


    /**
     * 查看佳人详情
     * @param
     * @param userId
     * @return
     */
    public RecommendUser queryRecommendFriend(Long userId, Long toUserId) {

        Criteria criteria = Criteria.where("userId").is(userId).and("toUserId").is(toUserId);
        Query query = Query.query(criteria);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        if(recommendUser == null){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
            recommendUser.setScore(99d);
        }
        return recommendUser;
    }


    /**
     *
     *卡片的推荐好友数据列表
     * 1.排除已喜欢或已不喜欢的用户
     * 2.构建查询推荐好友的条件
     * 3.
     */
    public List<RecommendUser> lookCardsList(Long userId, int count) {

        //1.根据用户的id查询用户已喜欢或不喜欢的用户,操作哦的是user_like表
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        //2.提取里面已喜欢或已不喜欢的用户id,用户已喜欢或已不喜欢的用户的id在User_like表是likeUserId字段
                                                                   //在好友推荐表里是userId字段
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);


        //3.构造查询推荐好友的条件,排除已喜欢或不喜欢的用户
        Criteria nin = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIds);//查询全部但不包括
        //4.统计函数，随机推荐好友
        TypedAggregation<RecommendUser> aggregation = TypedAggregation.newAggregation(RecommendUser.class,
                                                                       Aggregation.match(nin),//查询条件，注意，这个查询条件不能写成上面那个criteria
                                                                       Aggregation.sample(count));//

        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(aggregation, RecommendUser.class);
        List<RecommendUser> mappedResults = results.getMappedResults();

        return mappedResults;
    }


}
